package com.clkj.aimin.controller;

import com.clkj.aimin.pojo.doctor.DoctorTalk;
import com.clkj.aimin.pojo.common.Result;
import com.clkj.aimin.pojo.common.StatusCode;
import com.clkj.aimin.pojo.topic.Topic;
import com.clkj.aimin.pojo.topic.TopicComments;
import com.clkj.aimin.pojo.user.Task;
import com.clkj.aimin.service.doctor.DoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/04/02 18:03:18
 **/
@RestController
@RequestMapping("/main")
@CrossOrigin
@Api(value = "/main", tags = {"首页"})

public class MainController {
    @Autowired
    private DoctorService doctorService;
    @PostMapping("/getMain")
    @ApiOperation(value = "app --获取首页信息")
    public Result getMain(@ApiParam(name="userType",value="用户身份")@RequestParam("userType") Integer userType,
                          @ApiParam(name="userId",value="用户id")@RequestParam("userId") Integer userId) {
        try {
            Map map = doctorService.getMain(userType,userId);
            return new Result(true, StatusCode.OK, "success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/getTopicCommonts")
    @ApiOperation(value = "app---获取文章/视频评论")
    public Result getTopicCommonts(Integer topicId,Integer userId) {
        try {
            Map map = doctorService.getTopicCommonts(topicId,userId);
            return new Result(true, StatusCode.OK, "success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/getDoctorTalk")
    @ApiOperation(value = "获取医生交流")
    public Result getDoctorTalk(Integer doctorId) {
        try {
            List<DoctorTalk> talks = doctorService.getDoctorTalk(doctorId);
            return new Result(true, StatusCode.OK, "success", talks);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/addComments")
    @ApiOperation(value = "app---添加文章或视频评论")
    public Result addComments(TopicComments comments) {
        try {
             doctorService.addComments(comments);
            return new Result(true, StatusCode.OK, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }
    @PostMapping("/addDoctorTalk")
    @ApiOperation(value = "app---添加医生交流")
    public Result addDoctorTalk(Integer doctorId, Integer userId,
                                @ApiParam(name = "talkDetail", value = "内容") @RequestParam("talkDetail") String talkDetail,
                                @ApiParam(name = "parentId", value = "回复的id") @RequestParam("parentId") Integer parentId,
                                @ApiParam(name = "userType", value = "0为用户说的，1为医生说的") @RequestParam("userType") Integer userType) {
        try {
             doctorService.addDoctorTalk(doctorId,userId,talkDetail,parentId,userType);
            return new Result(true, StatusCode.OK, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/listDoctorMain")
    @ApiOperation(value = "app--医生端首页(没用)")
    public Result listDoctorMain(Integer doctorId) {
        try {
            Map map = doctorService.listDoctorMain(doctorId);
            return new Result(true, StatusCode.OK, "success",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/listMoreTopic")
    @ApiOperation(value = "获取更多文章或视频列表")
    public Result listMoreTopic(@ApiParam(name = "doctorId", value = "如果是医生端，则传医生ID,否则传0") @RequestParam("doctorId") Integer doctorId,
                                @ApiParam(name = "topicType", value = "文章传0，视频为1") @RequestParam("topicType") Integer topicType) {
        try {
            List<Topic> topics  = doctorService.listMoreTopic(doctorId,topicType);
            return new Result(true, StatusCode.OK, "success",topics);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }
    @PostMapping("/taskList")
    @ApiOperation(value = "获取任务列表")
    public Result taskList() {
        try {
            List<Task> task  = doctorService.taskList();
            return new Result(true, StatusCode.OK, "success",task);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }
    @PostMapping("/updateTask")
    @ApiOperation(value = "更新任务")
    public Result updateTask(Task task) {
        try {
            doctorService.updateTask(task);
            return new Result(true, StatusCode.OK, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }
    @PostMapping("/addArticleOrVideo")
    @ApiOperation(value = "添加文章或视频")
    public Result addArticleOrVideo(Topic topic, HttpServletRequest request) {
        try {
            doctorService.addArticleOrVideo(topic,request);
            return new Result(true, StatusCode.OK, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }
    @PostMapping("/ArticleTop")
    @ApiOperation(value = "文章置顶,排序,上下架")
    public Result ArticleTop(String Ids, Integer zhiding, Integer orderNum, Integer topicStatus) {
        try {
            doctorService.ArticleTop(Ids,zhiding,orderNum,topicStatus);
            return new Result(true, StatusCode.OK, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/addPrise")
    @ApiOperation(value = "文章、视频点赞或取消")
    public Result addPrise(@ApiParam(name="userId",value ="用户id")@RequestParam("userId")Integer  userId,
                           @ApiParam(name="topicId",value ="文章/视频Id")@RequestParam("topicId")Integer topicId,
                           @ApiParam(name="flag",value ="0取消  1点赞")@RequestParam("flag")Integer flag ) {
        try {
            doctorService.addPrise(userId,topicId,flag);
            return new Result(true, StatusCode.OK, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

    @PostMapping("/topicDetail")
    @ApiOperation(value = "根据文章或视频获取文章详情")
    public Result topicDetail(@ApiParam(name="topicId",value ="文章/视频Id")@RequestParam("topicId")Integer topicId ) {
        try {
            Topic topic = doctorService.topicDetail(topicId);
            return new Result(true, StatusCode.OK, "success",topic);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "success");
        }
    }

}
